﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class LevelThanksPlayers : MonoBehaviour
{
    public GameLevelItem gameLevelItem;
    private int dragDis = 200;
    private float thanksTargetPos;
    private float playersTargetPos;
    private float beTargetPos;
    private float dTargetPos;
    public DragObjectY dragPlayers;
    public DragObjectY dragThanks;
    public DragObjectX dragBe;
    public DragObjectX dragD;
    private bool levelFinished = false;

    public uTools.uTweenAlpha tweenAlpha;
    void Start()
    {
        dragThanks.minY = dragThanks.transform.position.y;
        thanksTargetPos = dragThanks.minY + dragDis;
        dragThanks.maxY = thanksTargetPos;

        dragPlayers.maxY = dragPlayers.transform.position.y;
        playersTargetPos = dragPlayers.maxY - dragDis;
        dragPlayers.minY = playersTargetPos;

        dragBe.maxX = dragBe.transform.position.x;
        beTargetPos = dragBe.maxX - dragDis;
        dragBe.minX = beTargetPos;

        dragD.minX = dragD.transform.position.x;
        dTargetPos = dragD.minX + dragDis;
        dragD.maxX = dTargetPos;

        dragThanks.endDragEvent.AddListener(CheckResult);
        dragPlayers.endDragEvent.AddListener(CheckResult);
        dragBe.endDragEvent.AddListener(CheckResult);
        dragD.endDragEvent.AddListener(CheckResult);

    }

    void CheckResult()
    {
        if (levelFinished) return;
        bool result = true;
        if (Mathf.Abs(dragThanks.transform.position.y - thanksTargetPos) > 2)
            result = false;
        if (Mathf.Abs(dragPlayers.transform.position.y - playersTargetPos) > 2)
            result = false;
        if (Mathf.Abs(dragBe.transform.position.x - beTargetPos) > 2)
            result = false;
        if (Mathf.Abs(dragD.transform.position.x - dTargetPos) > 2)
            result = false;
        if (result)
        {
            levelFinished = true;
            tweenAlpha.enabled = true;
            tweenAlpha.DOPlayForward();
            tweenAlpha.onFinished.AddListener(() =>
            {
                gameLevelItem.TriggerResult(true);
            });
        }
    }


}
